package com.redoor.app.stake.basicdata.mq.sendService;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.redoor.app.stake.basicdata.model.BaseConsts;
import com.redoor.app.stake.basicdata.model.constants.BasePropConstants;
import com.redoor.app.stake.basicdata.service.BaseConstsService;
import com.redoor.app.stake.basicdata.service.BasePropRedisService;
import com.redoor.app.stake.basicdata.service.BasePropService;
import com.redoor.app.stake.common.model.CompareResultBean;
import com.redoor.app.stake.common.utils.CompareUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
 * Created by wangjunlei on 2018/3/3.
 */
public class AbsSendMQService {

    public Producer producer ;

    private Logger logger = LoggerFactory.getLogger(AbsSendMQService.class);

    @Autowired
    private BaseConstsService baseConstsService;

    @Autowired
    private BasePropService basePropService;

    @PostConstruct
    public void  init(){
        logger.debug("init mq service impl");
        try{
            Properties cprop = new Properties();
            cprop.put(PropertyKeyConst.ProducerId, this.basePropService.getProp(BasePropConstants.producerId));
            cprop.put(PropertyKeyConst.AccessKey, this.basePropService.getProp(BasePropConstants.accesskey));
            cprop.put(PropertyKeyConst.SecretKey, this.basePropService.getProp(BasePropConstants.secretkey));
            cprop.put(PropertyKeyConst.SendMsgTimeoutMillis, 3000);
            cprop.put(PropertyKeyConst.ONSAddr, this.basePropService.getProp(BasePropConstants.namesrvAddr));
            if(basePropService.initMQFlag()) {
                producer = ONSFactory.createProducer(cprop) ;
                producer.start() ;
                logger.info("init mq service producer successfully") ;
            } else {
                logger.warn("not init service producer because flag is flase");
            }
        } catch (Exception e){
            logger.error("init service producer failure",e);
        }
    }

    /**
     * 对比两个类的属性判断是否需要发送MQ
     * @param oldObject 旧类对象
     * @param newObject 新类对象
     * @param const_code 属性码对应的属性列表集合
     * @return
     */
    public boolean needSendMQ(Object oldObject, Object newObject,String const_code){
        boolean need = false;
        try{
            List<BaseConsts> constsList = baseConstsService.getConstsList(const_code);
            Set<String> attrSet = new HashSet<>();
            for (BaseConsts cons:constsList) {
                attrSet.add(cons.getConstvalue());
            }
            //map2.put("name", field1[i].getName());
            //map2.put("old", field1[i].get(class1));
            //map2.put("new", field2[j].get(class2));

            List<CompareResultBean> attrList = CompareUtil.compareTwoClass(newObject,oldObject,null);
            for (CompareResultBean bean:attrList){
                String attrName = bean.getName();
                if(attrSet.contains(attrName.toUpperCase())){
                    need = true;
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage());
        }
        return need;
    }


    public boolean checkObjectChange(Object oldObject, Object newObject)  {
        List<CompareResultBean> attrList = null;
        try {
            attrList = CompareUtil.compareTwoClass(oldObject,newObject,null);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return attrList!=null && attrList.size()>0;
    }

}
